<?php

/**
 * Description of Encheres
 *
 * @author FFOZEU
 */

class Encheres extends DbFactory{
    
	protected $id;
	protected $nom;
	protected $prix;
	protected $enchere;
	protected $images;
	protected $duree;
	protected $user_id;
	protected $reserve;
	protected $limite;
	protected $autoEnchere;
	protected $robots;
	protected $description;
	protected $une;
	protected $ordre;
	protected $sent;
	protected $user_email;
	protected $user_nom;
	protected $user_prenom;
	protected $time;
	protected $retime;
	protected $credits;
	protected $minBid;
	protected $maxBid;
	protected $bid_id;
    protected $ae;
    protected $pause;
    protected $dateFin;
	public $liste;
	public $detail;
    protected $meneur;
    protected $pseudo;
    protected $terminee;
    protected $dateDebut;
    protected $begin;
	protected $nameTable ='c2w_encheres';

    //SETTERS
	public function setId($id) {
	
		$this->id = $id;
	
	}
	
	public function setNom($nom) {
	
		$this->nom = $nom;
	
	}
	
	public function setPrix($prix) {
	
		$this->prix = $prix;
	
	}
	
	public function setImages($image) {
	
		$this->images = $image;
	
	}
	
	public function setDuree($duree) {
	
		$this->duree = $duree;
	
	}
	
	public function setReserve($reserve) {
	
		$this->reserve = $reserve;
	
	}
	
	public function setLimite($limite) {
	
		$this->limite = $limite;
	
	}
	
	public function setAutoEnchere($autoEnchere) {
	
		$this->autoEnchere = $autoEnchere;
	
	}
	
	public function setRobots($robots) {
	
		$this->robots = $robots;
	
	}
	
	public function setDescription($description) {
	
		$this->description = $description;
	
	}
	
	public function setUne($une) {
	
		$this->une = $une;
	
	}
	
	public function setOrdre($ordre) {
	
		$this->ordre = $ordre;
	
	}
	
	public function setSent($sent) {
	
		$this->sent = $sent;
	
	}
	
	public function setUserId($user_id) {
	
		$this->user_id = $user_id;
	
	}
	
	public function setTime($time) {
	
		$this->time = $time;
	
	}
	
	public function setCredits($credits) {
	
		$this->credits = $credits;
	
	}
	
	public function setMinBid($minBid) {
	
		$this->minBid = $minBid;
	
	}
	
	public function setMaxBid($maxBid) {
	
		$this->maxBid = $maxBid;
	
	}
    
    public function setRetime($retime) {
	
		$this->retime = $retime;
	
	}
    
    public function setAe($ae) {
	
		$this->ae = $ae;
	
	}
    
    public function setPause($pause) {
	
		$this->pause = $pause;
	
	}
    
    public function setEnchere($enchere) {
	
		$this->enchere = $enchere;
	
	}
    
    public function setDateFin($dateFin) {
	
		$this->dateFin =$dateFin ;
	
	}
    
    public function setDateDebut($dateDebut) {
	
		$this->dateDebut =$dateDebut ;
	
	}
    
    public function setMeneur($meneur) {
	
		$this->meneur =$meneur ;
	
	}
    public function setPseudo($pseudo) {
	
		$this->pseudo =$pseudo ;
	
	}
    public function setTerminee($terminee) {
	
		$this->terminee =$terminee ;
	
	}
    public function setBegin($begin) {
	
		$this->begin =$begin ;
	
	}
    //GETTERS
    public function getId() {
	
		return $this->id ;
	
	}
	
	public function getNom() {
	
		return $this->nom ;
	
	}
	
	public function getPrix() {
	
		return $this->prix ;
	
	}
	
	public function getImages() {
	
		return $this->images ;
	
	}
	
	public function getDuree() {
	
		return $this->duree ;
	
	}
	
	public function getReserve() {
	
		return $this->reserve ;
	
	}
	
	public function getLimite() {
	
		return $this->limite ;
	
	}
	
	public function getAutoEnchere() {
	
		return $this->autoEnchere ;
	
	}
	
	public function getRobots() {
	
		return $this->robots ;
	
	}
	
	public function getDescription() {
	
		return $this->description ;
	
	}
	
	public function getUne() {
	
		return $this->une ;
	
	}
	
	public function getOrdre() {
	
		return $this->ordre ;
	
	}
	
	public function getSent() {
	
		return $this->sent ;
	
	}
	
	public function getUserId() {
	
		return $this->user_id ;
	
	}
	
	public function getTime() {
	
		return $this->time;
	
	}
	
	public function getCredits() {
	
		return $this->credits;
	
	}
	
	public function getMinBid() {
	
		return $this->minBid;
	
	}
	
	public function getMaxBid() {
	
		return $this->maxBid;
	
	}
    
    public function getRetime() {
	
		return $this->retime;
	
	}
    
    public function getAe() {
	
		return $this->ae;
	
	}
    
    public function getPause() {
	
		return $this->pause;
	
	}
    
    public function getEnchere() {
	
		return $this->enchere;
	
	}
    
    public function getDateFin() {
	
		return $this->dateFin;
	
	}
    
    public function getDateDebut() {
	
		return $this->dateDebut;
	
	}
    
    public function getMeneur() {
	
		return $this->meneur;
	
	}
    
    public function getPseudo() {
	
		return $this->pseudo;
	
	}
    public function getTerminee() {
	
		return $this->terminee;
	
	}
    public function getBegin() {
	
		return $this->begin ;
	
	}
	
	public function getEnchereUne(){
        
        $sql='SELECT e.id, e.nom, e.prix, e.enchere, e.images, e.dateFin, e.reserve, e.limite, e.retime, e.termine, e.pause, e.meneur, e.begin, e.duree, u.pseudo
              FROM '.$this->nameTable.' AS e
              LEFT JOIN c2w_utilisateurs AS u ON e.meneur = u.id
              WHERE e.une = "1" AND termine = "0" AND e.dateFin > "'.date('Y-m-d H:i:s').'" 
              ORDER BY dateFin DESC, e.ordre
              LIMIT 1';
        $requete = DbFactory::getPdoInstance()->query($sql);
		$tabArray =array();
        foreach( $requete->fetchAll(PDO::FETCH_ASSOC) as $result ) {
		
			$tabArray[] = $result;
	
		}
        return $tabArray;
    }
	
	function updateBeginEnchere(array $param){
        $sql = 'UPDATE '.$this->nameTable.' SET begin = 1, dateFin = :dateFin WHERE id = :id';
        
        $req=DbFactory::getPdoInstance()->prepare($sql);
        $req->bindValue(':dateFin',$param['dateFin']);
        $req->bindValue(':id',intval($param['id']));
        
        return $req->execute();
    }
    
	public function getEnchereEnCours(){
        $sql = 'SELECT e.id, e.nom, e.prix, e.enchere, e.images, e.dateFin, e.reserve, e.limite, e.retime, e.termine, e.pause, e.meneur, e.begin, e.duree, u.pseudo
                FROM '.$this->nameTable.' AS e
                LEFT JOIN c2w_utilisateurs AS u ON e.meneur = u.id
                WHERE e.dateFin > "'.date('Y-m-d H:i:s').'" AND e.une = "0" AND e.termine = "0"
                ORDER BY e.id, e.ordre
                LIMIT ' . _ENCHERES_EN_COURS_;
        
        $obj=DbFactory::getPdoInstance()->query($sql);
        
        $tabArray =array();
        foreach( $obj->fetchAll(PDO::FETCH_ASSOC) as $result ) {
		
			$tabArray[] = $result;
	
		}
        return $tabArray;
    }
	
	public function getEncheresAvenir(){
        $sql = 'SELECT e.id, e.nom, e.prix, e.enchere, e.images, e.dateDebut, e.reserve, e.limite, e.retime, e.termine, e.pause, e.meneur, e.begin, e.duree, u.pseudo
              FROM '.$this->nameTable.' AS e
              LEFT JOIN c2w_utilisateurs AS u ON e.meneur = u.id
              WHERE e.begin ="0"
              ORDER BY e.ordre, e.id';
        
        $obj=DbFactory::getPdoInstance()->query($sql);
        
        $tabArray =array();
        foreach( $obj->fetchAll(PDO::FETCH_ASSOC) as $result ) {
		
			$tabArray[] = $result;
	
		}
        return $tabArray;
    }
	
	public function getEncheresTerminees(){
        $sql = 'SELECT e.id, e.nom, e.prix, e.enchere, e.images, e.dateFin, e.reserve, e.limite, e.retime, e.termine, e.pause, e.meneur, u.pseudo
              FROM '.$this->nameTable.' AS e
              LEFT JOIN c2w_utilisateurs AS u ON e.meneur = u.id
              WHERE "'.date('Y-m-d H:i:s').'" >= e.dateFin AND e.begin ="1"
              ORDER BY dateFin DESC, e.ordre';
        
        $obj=DbFactory::getPdoInstance()->query($sql);
        
        $tabArray =array();
        foreach( $obj->fetchAll(PDO::FETCH_ASSOC) as $result ) {
		
			$tabArray[] = $result;
	
		}
        return $tabArray;
    }
    
    /**
     *reccupère une enchere a partir de son Id
     * @param type $ids
     * @return type 
     */
    public function getEnchereById($ids){
        $sql='SELECT e.id, e.nom, e.prix, e.images, e.enchere, e.dateFin, DATE_FORMAT(dateFin, "%Y/%m/%d %H:%i") as fin, e.duree, e.reserve, e.limite, e.retime, e.ae, e.robots, e.description, e.pause, e.meneur, e.termine, e.begin, e.sent, u.pseudo
              FROM '.$this->nameTable.' AS e
              LEFT JOIN c2w_utilisateurs AS u ON e.meneur = u.id
              WHERE e.id = :id';
        
        $obj=DbFactory::getPdoInstance();
        $detail = $obj->prepare($sql);
		$detail->bindParam(':id', intval($ids));
		$detail->execute();
		//$obj=DbFactory::getPdoInstance()->query($sql);
        
        $tabArray =array();
        foreach( $detail->fetchAll(PDO::FETCH_ASSOC) as $result ) {
		
			$tabArray[] = $result;
	
		}
        return $tabArray;
    }
}

?>
